﻿window.onload = function(){
	// verify
	verify();
	
	var ph = document.getElementsByClassName("placeholder");
	for(var i = 0; i < ph.length; i++)
	{
		if('placeholder' in document.createElement('input'))
		{
			break;
		}
		ph[i].parentNode.childNodes[0].style.display = "block";
		ph[i].onkeyup = function(){
			if(this.value != "") this.parentNode.childNodes[0].style.display = "none";
			else this.parentNode.childNodes[0].style.display = "block";
		};
		ph[i].onfocus = function(){
			if(this.value != "") this.parentNode.childNodes[0].style.display = "none";
			else this.parentNode.childNodes[0].style.display = "block";
		};
		ph[i].onblur = function(){
			if(this.value != "") this.parentNode.childNodes[0].style.display = "none";
			else this.parentNode.childNodes[0].style.display = "block";
		};
	}

};

disabledbtn = function(disable){
	var btn = document.getElementById("sb");
	if(disable){
		btn.style.margin = "0";
		btn.style.width = "100%";
		btn.style.backgroundColor = "#CCC";
		btn.disabled = true;
		document.getElementById("loading").style.display = "block";
	}
	else
	{
		btn.style.margin = "";
		btn.style.width = "";
		btn.style.backgroundColor = "";
		btn.disabled = false;
		document.getElementById("loading").style.display = "none";

	}
}

verify = function(){
	document.getElementById("number").onblur = function(){
		var Meg = new MessageBox(this, 1, "学号不能为空..", 100);
		if(this.value == "") Meg.Show(); 
		else{
			Meg.Remove();
			Meg.message = "该学号已经报过名..";
			Ajax("./ajax/checkrepeat.php?number=" + this.value, Request(Meg));
		}
		
	};
	document.getElementById("name").onblur = function(){
		var Meg = new MessageBox(this, 2, "姓名不能为空..", 100);
		if(this.value == "") Meg.Show();
		else{
			Meg.Remove();
		}
	};
	document.getElementById("email").onblur = function(){
		var Meg = new MessageBox(this, 3, "邮箱不能为空..", 100);
		if(this.value == "") Meg.Show();
		else{
			Meg.Remove();
			Meg.message = "该邮箱已报过名..";
			Ajax("./ajax/checkrepeat.php?email=" + this.value, Request(Meg));
		}
	};
	document.getElementById("class").onblur = function(){
		var Meg = new MessageBox(this, 3, "班级不能为空..", 100);
		if(this.value == "") Meg.Show();
		else Meg.Remove();
	};
	document.getElementById("telphone").onblur = function(){
		var Meg = new MessageBox(this, 3, "电话不能为空..", 100);
		if(this.value == "") Meg.Show();
		else Meg.Remove();
	};
	document.getElementById("sb").onclick = function(){
		var Meg = new MessageBox(null, 4, "该项不能为空..", 100);
		Meg.Remove();
		var objs = document.getElementsByClassName("required");
		for(var i = 0; i <objs.length; i++)
		{
			if(objs[i].isEmpty())
			{
				Meg.element = objs[i];
				Meg.Show();
				return false;
			}
		}
		disabledbtn(true);
		return !Ajax("./ajax/checkrepeat.php?email=" + document.getElementById("email").value + 
		"&number=" + document.getElementById("number").value + 
		"&name=" + document.getElementById("name").value, function(responseText){
			var res = responseText;
			if(res.indexOf("false") >= 0)
			{
				document.getElementById("joinform").submit();
				return false;
			}
			disabledbtn(false);
			if(res.split(':').length == 2 && res.split(':')[1].indexOf("email") >= 0)
			{
				var Meg = new MessageBox(document.getElementById("email"), 3, "该邮箱已报过名..", 100);
				Meg.Show();
			}
			else if(res.split(':').length == 2 && res.split(':')[1].indexOf("number") >= 0)
			{
				var Meg = new MessageBox(document.getElementById("number"), 2, "该学号已报过名..", 100);
				Meg.Show();
			}
			/*else if(res.split(':').length == 2 && res.split(':')[1].indexOf("name"))
			{
				var Meg = new MessageBox(document.getElementById("name"), 1, "该姓名已报过名..", 100);
				Meg.Show();
			}*/
			return true;
		});
	};
	
}

Request = function(Box){
	return function(responseText){
		var res = responseText;
		if(res.indexOf("true") >= 0)
		{
			Box.Show();
			disabledbtn(false);
		}
		else
		{
			Box.Remove();
		}
		return true;
	};
};

	// Name : function(){
		// var eName = document.getElementById("name");
		// var Meg = new MessageBox(eName, 2, "该姓名已报过名..", 100);
		// return function(responseText){
			// var res = responseText;
			// if(res.indexOf("true") >= 0)
			// {
				// Meg.Show();
			// }
			// else
			// {
				// Meg.Remove();
			// }
			// return true;
		// };
	// },

	// Email : function(){
		// var eEmail = document.getElementById("email");
		// var Meg = new MessageBox(eEmail, 3, "该邮箱已报名..", 100);
		// return function(responseText){
			// var res = responseText;
			// if(res.indexOf("true") >= 0)
			// {
				// Meg.Show();
			// }
			// else
			// {
				// Meg.Remove();
			// }
			// return true;
		// };
	// }
// }